package dao.impl;

import dao.UserDao;
import entity.User;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import utils.JDBCUtils;

import java.util.List;

/**
 * @program: FinalExam
 * @description:
 * @create: 2021-12-14 17:11
 * @Date 2021/12/14
 **/

public class UserDaoImpl implements UserDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    public User adminLogin(String username, String password) {
        User user = null;
        try {
            String sql = "select * from user where username = ? and password = ? and status = ?";
            user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), username, password,1);
        } catch (DataAccessException e) {

        }
        return user;
    }

    public User userLogin(String username, String password) {
        User user = null;
        try {
            String sql = "select * from user where username = ? and password = ? and status = ? ";
            user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), username, password,0);
        } catch (DataAccessException e) {

        }
        return user;
    }

    public User login(String username, String password) throws DataAccessException {
        User user = null;
        try {
            String sql = "select * from user where username = ? and password = ? ";
            user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), username, password);
        } catch (DataAccessException e) {

        }

        return user ;
    }

    public Boolean addUser(User user) {
        int update = 0;
        try {
            String sql =
                    "insert into user (id,username,password," +
                            "realname,gmt_create,gmt_modify" + ",is_delete,status) " +
                            "values (?,?,?,?,?,?,?,?)";
            update = template.update(sql, user.getId(), user.getUsername(), user.getPassword()
                    , user.getRealname(), user.getGmt_create(), user.getGmt_modify(), user.getIs_delete()
                    , user.getStatus());
        } catch (DataAccessException e) {

        }
        return update>0;
    }

    public Boolean deleteUserById(String id,String gmt_modify) {

        int update = 0;
        try {
            String sql = "update user set gmt_modify = ? , is_delete = ? where id = ? ";
            update = template.update(sql, gmt_modify,1,id);
        } catch (DataAccessException e) {

        }
        return update > 0;
    }

    public Boolean modifyUserInfoById(String id, User user) {
        int update = 0;
        try {
            user.setId(id);
            String sql = "update user set id = ? , username = ? , password = ? ," +
                    "realname = ? ,status = ? ,gmt_create = ? ,gmt_modify = ?" +
                    ",is_delete = ? where id = ? ";
            update = template.update(sql, user.getId(), user.getUsername(), user.getPassword(), user.getRealname()
                    , user.getStatus(), user.getGmt_create(), user.getGmt_modify(), user.getId(), id);
        } catch (DataAccessException e) {

        }
        return update > 0 ;
    }

    public List<User> ShowUsersInfo() {
        List<User> list = null;
        try {
            String sql = "select * from user";
            list = template.query(sql, new BeanPropertyRowMapper<User>(User.class));
        } catch (DataAccessException e) {

        }
        return list;
    }

    public User findById(String id) {
        User user = null;
        try {
            String sql = "select * from user where id = ? ";
            user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class),id);
        } catch (DataAccessException e) {

        }
        return user;
    }

    public User findByUsername(String username) {
        User user = null;
        try {
            String sql = "select * from user where username  = ? ";
            user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), username);
        } catch (DataAccessException e) {

        }
        return user;
    }

    public User findByIdAndUsername(String id, String username) {
        User user = null;
        try {
            String sql = "select * from user where id = ? and username = ? ";
            user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), id, username);
        } catch (DataAccessException e) {

        }
        return user;
    }

    public void modifyPassword(String id, String password) {
        try {
            String sql = "update user set password = ? where id = ? ";
            template.update(sql,password,id);
        } catch (DataAccessException e) {

        }
    }

    public List<User> findAllUserInfo() {
        try {
            String sql = "select * from user where status = ? and is_delete = ?";
            return template.query(sql, new BeanPropertyRowMapper<User>(User.class),0,0);
        } catch (DataAccessException e) {

        }
        return null;
    }


}
